/*
 * SkinNode.cpp
 *
 *  Created on: 2012/03/16
 *      Author: takedanaoki
 */

#include "StdAfx.h"
#include "NTEngine/NTEngine.h"
#include "NTEngine/Base/RefString.h"

#include "Scene/src/SkinNode.h"
#include "Scene/src/ContainerImpl.h"

#include "NTEngine/Xml/Element.h"
#include "NTEngine/Xml/Attribute.h"

namespace NTEngine {
using namespace Xml;
namespace Scene {

SkinNode::SkinNode( Xml::ConstElement& e, Container::Impl& c, Matrix3x4* bones )
: mWeightNumber( 0 )
, mWeights( 0 )
, mVertexIndeices( 0 )
, mName( 0 )
, mBones( bones ) {
	ASSERT( RefString( "Node" ) == e.name() ); //Nodeだよね？
	ASSERT( e.childNumber() > 0 ); //Data一個はあるよね？
	mOffset.identity();
	//名前取得
	int an = e.attributeNumber();
	for ( int i = 0; i < an; ++i ){
		ConstAttribute a = e.attribute( i );
		RefString name( a.name() );
		if ( name == "name" ){
			mName = c.allocateString( RefString( a.value() ) );
		} else if ( name == "offset" ) {
			a.getFloatValues( &mOffset.m11, 12 );
		}
	}
	mWeightNumber = e.childNumber();
	mWeights = NEW float[ mWeightNumber ];
	mVertexIndeices = NEW int[ mWeightNumber ];
	for ( int i = 0; i < mWeightNumber; ++i ){
		ConstElement child = e.child( i );
		build( child, i );
	}
}

void SkinNode::build( Xml::ConstElement& e, int index ) {
	int an = e.attributeNumber();
	for ( int i = 0; i < an; ++i ) {
		ConstAttribute a = e.attribute( i );
		RefString name( a.name() );
		if ( name == "weight" ){
			mWeights[ index ] = a.getFloatValue();
		} else if ( name == "vertex" ) {
			mVertexIndeices[ index ] = a.getIntValue();
		}
	}
}

} // namespace Scene
} // namespace NTEngine
